package com.sinothk.fc.store.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sinothk.fc.store.entity.StoreEntity;
import com.sinothk.fc.store.entity.StoreHotGoodsVO;
import com.sinothk.fc.store.entity.StoreVO;
import com.sinothk.fc.store.service.IFcStoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * @Description: 招财猫门店
 * @Author: Liangyt
 * @Date: 2022-07-10
 * @Version: V1.0
 */
@Api(tags = "招财猫门店")
@RestController
@RequestMapping("/fc/storeCtrl")
@Slf4j
public class FcStoreController extends JeecgController<StoreEntity, IFcStoreService> {
    @Autowired
    private IFcStoreService fcStoreService;

    /**
     * 分页列表查询
     *
     * @param fcStore
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "招财猫门店-分页列表查询")
    @ApiOperation(value = "招财猫门店-分页列表查询", notes = "招财猫门店-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(StoreEntity fcStore,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<StoreEntity> queryWrapper = QueryGenerator.initQueryWrapper(fcStore, req.getParameterMap());
        Page<StoreEntity> page = new Page<StoreEntity>(pageNo, pageSize);
        IPage<StoreEntity> pageList = fcStoreService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加或编辑
     *
     * @param fcStore
     * @return
     */
    @AutoLog(value = "招财猫门店-添加或编辑")
    @ApiOperation(value = "招财猫门店-添加或编辑", notes = "招财猫门店-添加或编辑")
    @PostMapping(value = "/addOrEdit")
    public Result<?> addOrEdit(@RequestBody StoreEntity fcStore) throws Exception {
        return Result.OK(fcStoreService.addOrEdit(fcStore));
    }

    /**
     * 添加
     *
     * @param fcStore
     * @return
     */
    @AutoLog(value = "招财猫门店-添加")
    @ApiOperation(value = "招财猫门店-添加", notes = "招财猫门店-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody StoreEntity fcStore) {
        fcStoreService.save(fcStore);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param fcStore
     * @return
     */
    @AutoLog(value = "招财猫门店-编辑")
    @ApiOperation(value = "招财猫门店-编辑", notes = "招财猫门店-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody StoreEntity fcStore) {
        fcStoreService.updateById(fcStore);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "招财猫门店-通过id删除")
    @ApiOperation(value = "招财猫门店-通过id删除", notes = "招财猫门店-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        fcStoreService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "招财猫门店-批量删除")
    @ApiOperation(value = "招财猫门店-批量删除", notes = "招财猫门店-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.fcStoreService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "招财猫门店-通过id查询")
    @ApiOperation(value = "招财猫门店-通过id查询", notes = "招财猫门店-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        StoreEntity fcStore = fcStoreService.getById(id);
        if (fcStore == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(fcStore);
    }

    /**
     * 招财猫门店展示详情-通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "招财猫门店展示详情-通过id查询")
    @ApiOperation(value = "招财猫门店展示详情-通过id查询", notes = "招财猫门店展示详情-通过id查询")
    @GetMapping(value = "/queryShowById")
    public Result<?> queryShowById(@RequestParam(name = "id") String id) throws Exception {
        StoreVO fcStore = fcStoreService.getShowStoreInfo(id);
        if (fcStore == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(fcStore);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param fcStore
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, StoreEntity fcStore) {
        return super.exportXls(request, fcStore, StoreEntity.class, "招财猫门店");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, StoreEntity.class);
    }

    /**
     * 通过id查询
     *
     * @param storeId
     * @return
     */
    @AutoLog(value = "招财猫门店-通过id查询")
    @ApiOperation(value = "招财猫门店-通过id查询", notes = "招财猫门店-通过id查询")
    @GetMapping(value = "/queryStoreHotGoodsById")
    public Result<StoreHotGoodsVO> queryStoreHotGoodsByStoreId(@RequestParam(name = "storeId") String storeId) throws Exception {
        if (StringUtils.isEmpty(storeId)) throw new Exception("未找到对应数据");
        StoreHotGoodsVO storeHotGoods = fcStoreService.queryStoreHotGoodsByStoreId(storeId);
        if (storeHotGoods == null) throw new Exception("未找到对应数据");
        return Result.OK(storeHotGoods);
    }

    /**
     * 查询附近商家
     *
     * @param pageNo
     * @param pageSize
     * @param locLat
     * @param locLng
     * @param locDistrict
     * @return
     * @throws Exception
     */
    @AutoLog(value = "查询附近商家")
    @ApiOperation(value = "查询附近商家", notes = "查询附近商家")
    @GetMapping(value = "/queryStoreForNearby")
    public Result<IPage<StoreEntity>> queryStoreForNearby(
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "state", defaultValue = "0") Integer state,
            @RequestParam(name = "locLat") Double locLat,
            @RequestParam(name = "locLng") Double locLng,
            @RequestParam(name = "locDistrict", required = false) String locDistrict) throws Exception {
        IPage<StoreEntity> pageList = fcStoreService.queryStoreForNearby(new Page<>(pageNo, pageSize), locDistrict, locLat, locLng, state);
        if (pageList == null) throw new Exception("未找到对应数据");
        return Result.OK(pageList);
    }

    /**
     * 分页列表 - 当前用户的关联门店分页
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @AutoLog(value = "门店-当前用户的关联门店分页")
    @ApiOperation(value = "门店-当前用户的关联门店分页", notes = "门店-当前用户的关联门店分页")
    @GetMapping(value = "/storePageForCurrUser")
    public Result<?> storePageForCurrUser( //StoreEmployeeEntity storeEmployee,
                                           @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        IPage<StoreEntity> pageList = fcStoreService.storePageForCurrUser(new Page<>(pageNo, pageSize));
        return Result.OK(pageList);
    }

    /**
     * 切换用户门店
     *
     * @param storeId
     * @return
     */
    @AutoLog(value = "切换用户门店")
    @ApiOperation(value = "切换用户门店", notes = "切换用户门店")
    @GetMapping(value = "/changeStoreForUser")
    public Result<?> changeStoreForUser(@RequestParam(name = "storeId") String storeId) {
        StoreEntity store = fcStoreService.changeStoreForUser(storeId);
        return Result.OK(store);
    }

    /**
     * 添加 - 初始化门店
     *
     * @return
     */
    @AutoLog(value = "招财猫门店-初始化门店")
    @ApiOperation(value = "招财猫门店-初始化门店", notes = "招财猫门店-初始化门店")
    @PostMapping(value = "/initStore")
    public Result<?> initStore() {
        StoreEntity fcStore = fcStoreService.initStore();
        return Result.OK(fcStore);
    }
}
